package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.Pms_SkuInfo_Mapper;
import com.shop.mapper.Pms_Sku_Images_Mapper;
import com.shop.pojo.pms.Pms_Sku_Images;
import com.shop.pojo.pms.Pms_Sku_Info;
import com.shop.service.Pms_Sku_Images_Service;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class Pms_Sku_Images_ServiceImpl extends ServiceImpl<Pms_Sku_Images_Mapper,Pms_Sku_Images> implements Pms_Sku_Images_Service {
    @Autowired
    private Pms_Sku_Images_Mapper pms_sku_images_mapper;
    @Autowired
    private Pms_SkuInfo_Mapper pms_skuInfo_mapper;
    /*
     * @Description 根据skuid来查询sku下的全部图片
     * @param skuId
     * @return java.util.List<com.shop.pojo.pms.Pms_Sku_Images>
     * @author liusupeng
     */
    public List<Pms_Sku_Images> findAllSkuImgById(Long skuId) {

        QueryWrapper<Pms_Sku_Images> queryWrapper=new QueryWrapper<Pms_Sku_Images>();
        queryWrapper.eq("sku_id",skuId);
        return pms_sku_images_mapper.selectList(queryWrapper);
    }
    /*
     * @Description
     * 先根据上传的默认图片，然后去遍历查找默认图片的地址，然后去skuinfo表里去修改默认图片
     * 根据skuid来删除当前表中跟skuid一样的所有数据
     * 然后在通过skuid来进行批量添加图片
     * @param pms_sku_images
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/10 17:49
     */
@Transactional
    @Override
    public R exitSkuImages(List<Pms_Sku_Images> pms_sku_images) {

             pms_sku_images.forEach(x->{
                 if(x.getDefault_img()==1){
                     Pms_Sku_Info pms_sku_info=new Pms_Sku_Info();
                     pms_sku_info.setSku_id(x.getSku_id());
                     pms_sku_info.setSku_default_img( x.getImg_url());
                     pms_skuInfo_mapper.updateById(pms_sku_info);
                 }

             });


           QueryWrapper<Pms_Sku_Images> pms_sku_imagesQueryWrapper=new QueryWrapper<>();
    pms_sku_imagesQueryWrapper.eq("sku_id",pms_sku_images.get(0).getSku_id());
    pms_sku_images_mapper.delete(pms_sku_imagesQueryWrapper);

    boolean b = this.saveBatch(pms_sku_images);
    if(b){
        return R.success();
    }

    return R.error(500,"服务器异常");
    }
}
